Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 7 - Control Statements


If Statements

If statements allow you to define statements or groups of statements that are executed only in specific circumstances. Each If statement contains one or more Boolean expressions whose values can be either true or false. AppleScript executes the statements contained in the If statement only if the value of the Boolean expression is true.

If statements are also called conditional statements. Boolean expressions in If statements are also called tests.

The following example uses an If statement to control whether or not a particular dialog box is displayed:

if dependents > 2 then
   display dialog "You might need to file an extra form"end if
The If statement contains the Boolean expression dependents > 2. If the value of the Boolean expression is true, the Display Dialog command is executed. If the value of the Boolean expression is false, the Display Dialog command is not executed. (Display Dialog is a scripting addition command. For more information about the way it works, see the AppleScript Scripting Additions Guide.)

If statements can contain multiple tests. For example, the following statement contains three tests.

if ( x > y ) then
   set myMessage to " is greater than "else if ( x < y ) then
   set myMessage to " is less than "else
   set myMessage to " is equal to "end if
set myResult to (x as string) & myMessage & (y as string)
If the expression x > y is true, the value of the variable myMessage is set to " is greater than " and the If statement is finished. Control passes to the Set statement, which uses the value of the variable myMessage to set the value of another variable, called myResult. The value of myResult is a string such as "7 is greater than 5". If the first Boolean expression is false, the next expression, x < y, is evaluated with similar results.

An If statement can contain any number of Else If clauses; AppleScript looks for the first Boolean expression contained in an If or Else If clause that is true, executes the statements contained in its block (the statements between one Else If and the following Else If or Else clause), and then exits the If statement.

An If statement can also include a final Else clause. The statements in its block are executed if no other test in the If statement passes. For example, suppose the values of x and y in the previous example are both 112. The first two tests, x > y and x < y, fail. The value of the variable myMessage is set to " is equal to ", and the value of myResult is "112 is equal to 112".

If statements can be more elaborate, as in this example:

display dialog "How many dependents?" default answer ""set dependents to (text returned of result) as integer
display dialog "Have you ever been audited?" buttons  ¬
   {"No", "Yes"}

if button returned of result = "Yes" then
   set audit to true
else
   set audit to false
end if

if dependents < 9 and audit = false then
   display dialog "No extra forms are required."else if dependents < 9 and audit = true then
   display dialog "You might need to file an extra form."else --anything greater than 9
   display dialog "You will need to file an extra form."end if
The example shows how you can create a more complex Boolean expression with the help of Boolean operators, such as the And operator. The expression

dependents < 9 and audit = false
has two Boolean expressions as operands (dependents < 9, audit = false). If both expressions are true, the value of the entire expression is true. Other Boolean operators are Or (another binary operator; if either
of its operands is true, the entire expression is true), and Not (a unary operator; if its operand is true, the expression is false, and vice versa).
For more information about operators, see Chapter 6, "Expressions."


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996